if [ ! $1 ]
then
   echo "usage : \". boot-strap filename\""
   return 1
fi
if [ ! -e $1 ]
then
    echo "File $1 does not exist, aborting...."
else 
  if [ ! -e patches.zoo ]
  then
      echo "patches.zoo must be in this directory, aborting...."
  else 
    clear 

# Get the main source from the virgin distribution.
    echo "Extracting \"*.[cyh]\" and \"*.def\" from $1"
    tar xf $1 bash-1.10/*.[chy] bash-1.10/*.def
    cd bash-1.10
    mv ../patches.zoo .

# Scramble the directory structure into something almost logical.
    echo "Reorganizing directory structure"
    mkdir src					# where source will reside
    mv *.[cyh] src/				# put virgin source in src/
    mv builtins/ src/				# atomic move builtins sub-dir
# Extract our mods/patches from the patches zoo archive.
    echo "Extracting files from \"patches.zoo\""
    zoo x.// patches.zoo
    rm -r -f lib/readline/examples		# get ride of some baggage
    rm -r -f lib/readline/doc
    rm -r -f documentation
    rm -r -f support
    rm src/builtins/psize.c

# Apply Patches, check for errors.
    echo "Applying patches"

    cd lib/readline				# We do readline first
    patch <../../read.diff 			# patch it
    if [ -e *.rej ]; then
      for i in *.rej
        do 
          echo "Failed to patch $i correctly."	# report all errors
        done
      return 1					# and punt out of it
    fi
    rm *.orig 				# We made it, get rid of originals
    rm ../../read.diff			# and patch file    
    cd ../../
    
    cd src/builtins				# We do src/builtins next
    patch <../../built.diff 			# patch it
    if [ -e *.rej ]; then
      for i in *.rej
        do 
          echo "Failed to patch $i correctly."	# report all errors
        done
      return 1					# and punt out of it
    fi
    rm *.orig 				# We made it, get rid of originals
    rm ../../built.diff			# and patch file
    cd ../../ 	 

    cd src					# We do src last
    patch <../source.diff 			# patch it
    if [ -e *.rej ]; then
      for i in *.rej
        do 
          echo "Failed to patch $i correctly."	# report all errors
        done
      return 1					# and punt out of it
    fi
    rm *.orig 				# We made it, get rid of originals
    rm ../source.diff			# and patch file
    cd ../../

# move it all up a directory, 
    mv bash-1.10/* .
# Then get rid of the empty,
    rm -r -f bash-1.10		

# Wow it all seemed to work
    echo "
           Bash patch successful. You may now build a BASH!
         "
  fi
fi
